$(function(){
    getUserInfo()
    // 退出登录
    $('#logout').on('click',function(){
        layer.confirm('是否确认退出？', {icon: 3, title:'提示'}, function(index){
            //do something
            location.href = '/login.html'
            localStorage.removeItem('token');
          });
    })
})
// 这个是获取用户信息的，其他还用用，所以封装起来
function getUserInfo(){
    axios.defaults.baseURL ='http://www.liulongbin.top:3007'
    axios({
        method:'get',
        url:'/my/userinfo',
        headers: {Authorization:localStorage.getItem('token')},
    }).then(({data:res})=>{
        if (res.status != 0) {
            return layui.layer.msg(res.message);
            
        }
        // console.log(res);
        renderAvatar(res)
    })
    
}
function renderAvatar(res){
    let nn = res.data.nickname||res.data.username
    $('.welcome').html(nn)
    if(res.data.user_pic==null){
        // toUpperCase()将字母转换为大写
     $('.avatar-text').html(nn[0].toUpperCase())
     $('.layui-nav-img').hide()
    }else{
     $('.layui-nav-img').attr('src',res.data.user_pic)
     $('.layui-nav-img').show()
     $('.avatar-text').hide()
    }
}